﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Spatial;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeManagement
{
    public partial class TreeEditForm : Form,IRecordPoint
    {
        public Form EditWardFormParent
        {
            get;
            set;
        }
        public ActionType FormType
        {
            get;
            set;
        }
        public TreeEditForm()
        {
            InitializeComponent();
        }

        private void btnLoadFromMap_Click(object sender, EventArgs e)
        {
            if ((sender as Button).Tag == null)
            {
                viewPanel1.registerRecodePoint(this);
                (sender as Button).Tag = 1;
                (sender as Button).Text = "Xóa";
            }
            else
            {
                txtPoint.Text = "(";
            }
            viewPanel1.RecordedPoints.Clear();
            viewPanel1.Refresh();
        }
        
        public void notifyRecordPoint(Point point)
        {
            if(TMUtils.getValidTree(point, viewPanel1.RecordedPoints) != Point.Empty)
            {
            viewPanel1.RecordedPoints.Add(point);
            txtPoint.Text = TMUtils.addStringFromRecordedPoint(txtPoint.Text, point);
            viewPanel1.Refresh();
            }
        }

        public void drawRecordedPoint(Graphics g)
        {
            TMUtils.drawTreePoint(g, viewPanel1.RecordedPoints);
        }

        private void TreeEditForm_Load(object sender, EventArgs e)
        {
            if (ActionType.NEW_TREE == this.FormType)
            {
                button1.Text = "Thêm";
                txtID.Enabled = false;
                txtPoint.Text = "(";
            }

            using (var context = new TreeDBEntities())
            {
                cbbOganize.DataSource = context.ManagementOrganizations.ToList();
                cbbType.DataSource = context.TreeTypes.ToList();
                

                viewPanel1.Wards = context.Wards.ToList();
                viewPanel1.Roads = context.Roads.ToList();
                viewPanel1.Trees = context.Trees.ToList();
                viewPanel1.Refresh();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ActionType.NEW_TREE == this.FormType)
            {
                String strText = "POINT "  + txtPoint.Text ;
                DbGeometry geometry = DbGeometry.PointFromText(strText, 0);
                using (var context = new TreeDBEntities())
                {
                    context.Trees.Add(new Tree
                    {
                        Type = (cbbType.SelectedItem as TreeType).ID,                    
                        Height = Convert.ToInt32( txtHeight.Text),
                        Wide = Convert.ToInt32(txtWide.Text),
                        Organize = (cbbOganize.SelectedItem as ManagementOrganization).ID,
                        DatePlanting = dateTimePicker1.Value,
                        Point = geometry
                    });
                    context.SaveChanges();
                }
                (this.EditWardFormParent as TreeForm).reload();
                this.Close();
            }
        }
    }
}
